package com.wl.eduorder.controller;


import com.wl.connonutils.R;
import com.wl.eduorder.service.PayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author wl
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/eduorder/paylog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo") String orderNo){
        //返回信息，包含二维码地址，还有其他信息
        Map<String,Object> map = payLogService.createNavice(orderNo);
        System.out.println("-------返回二维码集合:"+map);
        return R.ok().data(map);
    }

    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable("orderNo") String orderNo){
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        if(null == map){
            return R.error().message("支付出错");
        }
        if(map.get("trade_state").equals("SUCCESS")){
            //支付成功,添加记录到支付表，更新订单状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }

}

